package com.baozou.framework.util;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Db extends SQLiteOpenHelper{

	private static String dbName = "BaoZouDb";
	private static int version = 2;
	private static String tableName = "Bookmark2";
	
	public Db(Context context) {
		super(context, dbName, null, version);
		// TODO Auto-generated constructor stub
	}

	public static String getTableName() {
		return tableName;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		String sql = "create table " + tableName + " (" +
					"_id integer primary key autoincrement," +
					"bk_name text," +
					"bk_index integer" +
					")";
		db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		String sql = "drop table if exists " + tableName;
		db.execSQL(sql);
		onCreate(db);
	}

	public void insert(ContentValues cv) {
		if (cv != null) {
			SQLiteDatabase db = this.getWritableDatabase();
			db.insert(tableName, null, cv);
		}
	}
	
	public void update(ContentValues cv){
		if (cv != null) {
			SQLiteDatabase db = this.getWritableDatabase();
			db.update(tableName, cv, "_id=?", new String[]{cv.get("_id").toString()});
		}
	}
	public void delete(ContentValues cv){
		if (cv != null) {
			SQLiteDatabase db = this.getWritableDatabase();
			db.delete(tableName, "_id=?", new String[]{cv.getAsString("_id")});
		}
	}
	
	public ContentValues queryById(int id){
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.query(tableName, null, "_id=?", new String[]{id+""}, null, null, null);
		ContentValues cv = null;
		if(c != null && c.getCount() != 0){
			cv = new ContentValues();
			c.moveToFirst();
			cv.put("_id", c.getInt(c.getColumnIndex("_id")));
			cv.put("bk_name", c.getString(c.getColumnIndex("bk_name")));
			cv.put("bk_index", c.getInt(c.getColumnIndex("bk_index")));
		}
		c.close();
		return cv;
	}
	
	public ContentValues queryCurrentIndex(){
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.query(tableName, null, "bk_name=?", new String[]{"current_index"}, null, null, null);
		ContentValues cv = null;
		if(c != null && c.getCount() != 0){
			cv = new ContentValues();
			c.moveToFirst();
			cv.put("_id", c.getInt(c.getColumnIndex("_id")));
			cv.put("bk_name", c.getString(c.getColumnIndex("bk_name")));
			cv.put("bk_index", c.getInt(c.getColumnIndex("bk_index")));
		}
		c.close();
		return cv;
	}
	public void deleteCurrentIndex(){
		SQLiteDatabase db = this.getReadableDatabase();
		db.delete(tableName, "bk_name=?", new String[]{"current_index"});
	}
	public List<ContentValues> queryAll(){
		List<ContentValues> list = new ArrayList<ContentValues>();
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.query(tableName, null, "bk_name<>?", new String[]{"current_index"}, null, null, null);
		if(c != null && c.getCount() != 0){
			for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
				ContentValues cv = new ContentValues();
				cv.put("_id", c.getInt(c.getColumnIndex("_id")));
				cv.put("bk_name", c.getString(c.getColumnIndex("bk_name")));
				cv.put("bk_index", c.getInt(c.getColumnIndex("bk_index")));
				
				list.add(cv);
			}
		}
		c.close();
		return list;
	}
}
